package com.dede.screensaverhelper.fragment

import android.content.Intent
import android.os.Bundle
import android.preference.Preference
import android.preference.PreferenceFragment
import android.preference.SwitchPreference
import android.provider.Settings
import android.view.View
import android.widget.ListView
import com.dede.screensaverhelper.Config
import com.dede.screensaverhelper.R
import com.dede.screensaverhelper.activity.DreamTempActivity
import com.dede.screensaverhelper.util.Utils
import org.jetbrains.anko.dip
import org.jetbrains.anko.startActivity

/**
 * @author hsh
 * @since 2018/2/1 16:34.
 * @doc
 */
class DreamFragment : PreferenceFragment(), Preference.OnPreferenceChangeListener,
        Preference.OnPreferenceClickListener {

    private val TAG = "DreamFragment"

    private val open: Preference by lazy { findPreference(Config.OPEN) }
    private val dreamEnable: SwitchPreference by lazy {
        findPreference(Config.DREAM_ENABLE) as SwitchPreference
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        addPreferencesFromResource(R.xml.dream)

        dreamEnable.onPreferenceChangeListener = this

        open.onPreferenceClickListener = this

        val openSetting = findPreference(Config.OPEN_SETTING)
        openSetting.onPreferenceClickListener = this

        val disableHome = findPreference(Config.DISABLE_HOME) as SwitchPreference
        disableHome.onPreferenceChangeListener = this
//        disableHome.onPreferenceClickListener = this
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val listView = view?.findViewById<ListView>(android.R.id.list)
        listView?.dividerHeight = dip(.5f)
    }

    override fun onResume() {
        super.onResume()
        open.summary = "当前屏保：${Utils.getScreenComponent(activity)}"

        val screenSaverState = Utils.getScreenSaverState(activity)
        dreamEnable.isChecked = screenSaverState
        open.isEnabled = screenSaverState
    }

    private fun dream() {
        startActivity<DreamTempActivity>()
    }

    override fun onPreferenceClick(preference: Preference?): Boolean {
        return when (preference?.key) {
            Config.OPEN -> {
                dream()
                true
            }
            Config.OPEN_SETTING -> {
                startActivity(Intent(Settings.ACTION_DREAM_SETTINGS))
                true
            }
            Config.DISABLE_HOME -> {
                false
            }
            else -> false
        }
    }

    override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
        return when (preference?.key) {
            Config.DREAM_ENABLE -> {
                if (newValue is Boolean) {
                    open.isEnabled = newValue
                    Utils.setScreenSaverState(activity, newValue)
                }
                true
            }
            Config.DISABLE_HOME -> {
                if (newValue is Boolean && newValue) {
                    val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
                    startActivity(intent)
                }
                true
            }
            else -> false
        }
    }

}